// pages/addNews/addNews.js
var app=getApp()
var add_news_url = "https://www.hkzhaoscut.top/manage/addNews"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text: "", //正文
    textNum: 0, //正文字数
    title: '', //标题

  },
  // 监听正文输入框
  textInput(e) {
    // console.log(e)
    var num = e.detail.cursor
    var input = e.detail.value
    this.setData({
      text: input,
      textNum: num
    })
  },
  //监听标题输入框
  titleInput(e) {
    var input = e.detail.value
    this.setData({
      title: input
    })
  },
  // 提交文章
  submit() {
    var that = this
    let text = that.data.text
    let title = that.data.title
    wx.request({
      url: add_news_url,
      header: {
        'Authorization': app.globalData.managerToken
      },
      data: {
        title: title,
        text: text
      },
      success(res) {
        console.log("添加新闻成功", res)
        wx.showToast({
          title: '新闻新增成功',
          icon:'none'
        })
        wx.setStorage({
          key: 'news',
          data: null,
        })
      }
    })
  },
  // 保存文章
  save() {
    var news = {}
    news.title = this.data.title
    news.text = this.data.text
    news.textNum = this.data.textNum
    wx.setStorage({
      key: 'news',
      data: news,
      success(res) {
        wx.showToast({
          title: '保存成功!',
          icon: 'none'
        }, 500)
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var news = wx.getStorageSync("news")
    console.log(news)
  },

})